<script setup>
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'

const router = useRouter()
const userStore = useUserStore()
const loading = ref(false)

const loginForm = reactive({
  username: 'admin',
  password: '123456',
  rememberMe: false
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}

const loginFormRef = ref(null)

const handleLogin = () => {
  loginFormRef.value.validate((valid) => {
    if (valid) {
      loading.value = true
      
      // 使用store的login方法
      userStore.login(loginForm)
        .then(() => {
          ElMessage.success('登录成功')
          router.push('/')
        })
        .catch(error => {
          ElMessage.error(error || '登录失败')
        })
        .finally(() => {
          loading.value = false
        })
    }
  })
}
</script>

<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <div class="logo">
          <img src="@/assets/vue.svg" alt="Logo" class="logo-img" />
          <h1 class="title">Vue3 管理系统</h1>
        </div>
        <div class="desc">基于Vue3 + Element Plus + Pinia的管理系统</div>
      </div>
      
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" label-width="0" size="large">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="用户名"
            prefix-icon="el-icon-user"
            clearable
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="密码"
            prefix-icon="el-icon-lock"
            show-password
            @keyup.enter="handleLogin"
          />
        </el-form-item>
        
        <div class="login-option">
          <el-checkbox v-model="loginForm.rememberMe">记住我</el-checkbox>
          <el-link type="primary">忘记密码?</el-link>
        </div>
        
        <el-form-item class="login-btn">
          <el-button :loading="loading" type="primary" @click="handleLogin" style="width: 100%">
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="other-login">
        <div class="divider">
          <span class="divider-text">其他登录方式</span>
        </div>
        <div class="icon-list">
          <el-tooltip content="QQ登录" placement="bottom">
            <i class="icon el-icon-chat-dot-square"></i>
          </el-tooltip>
          <el-tooltip content="微信登录" placement="bottom">
            <i class="icon el-icon-chat-line-square"></i>
          </el-tooltip>
          <el-tooltip content="GitHub登录" placement="bottom">
            <i class="icon el-icon-position"></i>
          </el-tooltip>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="less" scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f0f2f5;
  background-image: url('');
  background-repeat: no-repeat;
  background-size: cover;
  
  .login-box {
    width: 400px;
    padding: 40px;
    background-color: #fff;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    
    .login-header {
      text-align: center;
      margin-bottom: 30px;
      
      .logo {
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: 10px;
        
        .logo-img {
          width: 40px;
          height: 40px;
        }
        
        .title {
          margin: 0 0 0 10px;
          font-size: 24px;
          font-weight: bold;
          color: #303133;
        }
      }
      
      .desc {
        color: #909399;
        font-size: 14px;
      }
    }
    
    .login-option {
      display: flex;
      justify-content: space-between;
      margin-bottom: 20px;
    }
    
    .login-btn {
      margin-bottom: 20px;
    }
    
    .other-login {
      margin-top: 30px;
      
      .divider {
        display: flex;
        align-items: center;
        margin: 20px 0;
        
        &:before,
        &:after {
          content: '';
          flex: 1;
          height: 1px;
          background: #e4e7ed;
        }
        
        .divider-text {
          padding: 0 10px;
          color: #909399;
          font-size: 12px;
        }
      }
      
      .icon-list {
        display: flex;
        justify-content: center;
        
        .icon {
          font-size: 24px;
          margin: 0 15px;
          color: #909399;
          cursor: pointer;
          transition: all 0.3s;
          
          &:hover {
            color: #409eff;
            transform: scale(1.2);
          }
        }
      }
    }
  }
}
</style> 